﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Collections;
/**
 *  @author : Tran Khai Hoang 
 *  @company : Ton Duc Thang university
 */
namespace EcoreSort
{
  
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Ecore file|*.ecore|All files|*.*";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
                txtEcoreFilePath.Text = openFileDialog1.FileName;
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                FileStream ecoreFileStream = new FileStream(txtEcoreFilePath.Text, FileMode.Open);
                FileInfo ecoreFileInfo = new FileInfo(txtEcoreFilePath.Text);
                String sortedEcoreFileName = ecoreFileInfo.DirectoryName + Path.DirectorySeparatorChar
                                             + ecoreFileInfo.Name.Replace("ecore","sort.ecore");
                XmlTextReader ecoreReader = new XmlTextReader(ecoreFileStream);
                ecoreReader.Read();
                XmlDocument ecoreDoc = new XmlDocument();
                XmlDocument sortedEcoreDoc = new XmlDocument();
                ecoreDoc.Load(ecoreReader);
                //Get ecore package
                XmlNodeList packageNodes = ecoreDoc.GetElementsByTagName("ecore:EPackage");
                foreach(XmlNode packageNode in packageNodes)
                {
                    XmlNodeList classNodes = packageNode.ChildNodes;
                    ArrayList nodesArray = new ArrayList();
                    foreach (XmlNode classNode in classNodes)
                        nodesArray.Add(classNode);
                    IComparer  nodeComparer;
                    if (radSortAsc.Checked)
                        nodeComparer = new XmlNodeAscendingComparer();
                    else
                        nodeComparer = new XmlNodeDescendingComparer();
                    nodesArray.Sort(nodeComparer);
                    // Create an XML declaration. 
                    XmlDeclaration xmldecl;
                    xmldecl = sortedEcoreDoc.CreateXmlDeclaration("1.0", null, null);
                    xmldecl.Encoding = "UTF-8";
                    sortedEcoreDoc.AppendChild(xmldecl);
                    //Append package node
                    XmlNode sortedPackageNode = sortedEcoreDoc.CreateElement("ecore:EPackage");
                    //Add attribute to package 
                    foreach (XmlAttribute attr in packageNode.Attributes)
                    { 
                        XmlAttribute sattr = sortedEcoreDoc.CreateAttribute(attr.Name);
                        sattr.InnerText = attr.InnerText;
                        sortedPackageNode.Attributes.Append(sattr);
                    }
                                         
                    //Add class nodes to package
                    foreach (XmlNode node in nodesArray)
                    {
                        XmlNode sortedClassNode = sortedEcoreDoc.ImportNode(node, true);
                        sortedPackageNode.AppendChild(sortedClassNode);
                    }
                    sortedEcoreDoc.AppendChild(sortedPackageNode);
                    
                }
                //Save sorted ecore file
                sortedEcoreDoc.Save(sortedEcoreFileName);
                //Fix some error
                StreamReader reader = new StreamReader(sortedEcoreFileName);
                String xmlText = reader.ReadToEnd();
                reader.Close();
                xmlText = xmlText.Replace("<EPackage version=\"2.0\"","<ecore:EPackage xmi:version=\"2.0\"");
                xmlText = xmlText.Replace("</EPackage>", "</ecore:EPackage>");
                StreamWriter writer = new StreamWriter(sortedEcoreFileName);
                writer.Write(xmlText);
                writer.Close();              

                MessageBox.Show("Ecore file sorted", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
            }catch(IOException)
            {
                MessageBox.Show("File not found","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
